<!DOCTYPE html>
<html>
<head>
    <title>数据库状态监控</title>
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        .alert-blink {
            animation: blinker 1s linear infinite;
        }
        @keyframes blinker {
            50% { opacity: 0.4; }
        }
    </style>
</head>
<body>
    
    <script>
        function updateStatus() {
            fetch('/api/status')
                .then(response => response.json())
                .then(data => {
                    const container = document.getElementById('status-list');
                    const detailBox = document.querySelector('.details-box');
                    const detailContent = document.getElementById('detail-content');
                    // 添加空状态提示
                    if (!data.data.some(item => item.status.includes('Failed'))) {
                        container.innerHTML = `
                            <div class="alert alert-success d-flex align-items-center">
                                <i class="fa-solid fa-circle-check me-3 fs-4"></i>
                                <div>
                                    <h5 class="mb-0">所有数据库运行正常</h5>
                                    <small class="text-muted">最后检查时间: ${new Date().toLocaleString()}</small>
                                </div>
                            </div>
                        `;
                        detailBox.style.display = 'none';
                        return;
                    }

                    container.innerHTML = data.data.filter(item => item.status.includes('Failed')).map(item => 
                        `<a href="#" class="list-group-item list-group-item-${item.status.includes('Failed') ? 'danger alert-blink' : 'success'} position-relative">
                            <i class="fa-solid fa-triangle-exclamation me-2 ${item.status.includes('Failed') ? '' : 'd-none'}"></i>
                            <span>${item.db_name}</span> @ ${item.check_time}: ${item.status}
                            <div class="badge bg-danger float-end ${item.status.includes('Failed') ? '' : 'd-none'}">CRITICAL</div>
                        </a>`
                    ).join('');

                    // 添加点击事件处理
                    document.querySelectorAll('.list-group-item').forEach(item => {
                        item.addEventListener('click', function(e) {
                            e.preventDefault();
                            const dbName = this.querySelector('span').innerText;
                            const targetRecord = data.data.find(r => r.db_name === dbName);
                            if (targetRecord.details) {
                                detailContent.textContent = targetRecord.details;
                                detailBox.style.display = 'block';
                            }
                        });
                    });
                });
        }

        // 初始更新
        updateStatus();
        // 每30秒更新一次
        setInterval(updateStatus, 30000);
    </script>

    <nav class="navbar navbar-expand-lg navbar-dark bg-dark mb-4">
      <div class="container">
        <a class="navbar-brand" href="{{ url_for('index') }}">数据库监控</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
          <ul class="navbar-nav">
            <li class="nav-item">
              <a class="nav-link active" href="{{ url_for('index') }}">实时状态</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="{{ url_for('history') }}">历史记录</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="{{ url_for('failures') }}">失败记录</a>
            </li>
          </ul>
        </div>
      </div>
    </nav>
    <div class="container mt-4">
        <h2>实时数据库状态</h2>
        <div id="status-list" class="list-group"></div>
    </div>
    <!-- 在实时状态卡片底部添加详情区域 -->
    <div class="details-box" style="display: none;">
        <h4>异常详情：</h4>
        <pre id="detail-content"></pre>
    </div>
</body>
</html>
<script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>